﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using NetCMS.DALFactory;
using NetCMS.Model;
using System.Text.RegularExpressions;
using System.Text;
using System.Reflection;
using NetCMS.DALProfile;
using NetCMS.Config;
namespace NetCMS.DALSQLServer
{
    public class Survey : DbBase, ISurvey
    {
        #region ManageVote.aspx
        public DataTable sel_voteById(int idt, int flag)
        {
            #region
            string Sql = null;
            if (flag == 0)//setClass.aspx
            {
                Sql = "Select vid,ClassName,Description From " + Pre + "vote_Class where SiteID='" + NetCMS.Global.Current.SiteID + "' and vid=" + idt;
            }
            else if (flag == 1)//setClass.aspx
            {
                Sql = "Select TID From " + Pre + "Vote_Topic where vid=" + idt + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 2)//setItem.aspx
            {
                Sql = "Select TID,IID,ItemName,ItemMode,PicSrc,DisColor,VoteCount,ItemDetail From " + Pre + "vote_Item where IID=" + idt + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 3)//setSteps.aspx
            {
                Sql = "Select SID,TIDS,Steps,TIDU From " + Pre + "vote_Steps where SiteID='" + NetCMS.Global.Current.SiteID + "' and SID=" + idt;
            }
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
            #endregion
        }
        public string GetVoteTitle(int tid)
        {
            string Sql = "Select [Title] From " + Pre + "Vote_Topic where SiteID='" + NetCMS.Global.Current.SiteID + "' and TID=" + tid;
            return Convert.ToString(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
        }
        public IList<VoteItemInfo> GetItemsByTopic(int tid)
        {
            IList<VoteItemInfo> list = new List<VoteItemInfo>();
            string SiteID = "0";
            if (!NetCMS.Global.Current.IsTimeout())
            {
                SiteID = NetCMS.Global.Current.SiteID;
            }
            string Sql = "Select [IID],[TID],[ItemName],[ItemMode],[PicSrc],[DisColor],[VoteCount],[ItemDetail],[SiteID] From " + Pre + "vote_Item where TID = " + tid + " and SiteID='" + SiteID + "' order by IID asc";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                VoteItemInfo info = new VoteItemInfo();
                info.IID = rd.GetInt32(0);
                info.TID = rd.GetInt32(1);
                if (!rd.IsDBNull(2)) info.ItemName = rd.GetString(2);
                if (!rd.IsDBNull(3)) info.ItemMode = rd.GetByte(3);
                if (!rd.IsDBNull(4)) info.PicSrc = rd.GetString(4);
                if (!rd.IsDBNull(5)) info.DisColor = rd.GetString(5);
                if (!rd.IsDBNull(6)) info.VoteCount = rd.GetInt32(6);
                if (!rd.IsDBNull(7)) info.ItemDetail = rd.GetString(7);
                if (!rd.IsDBNull(8)) info.SiteID = rd.GetString(8);
                list.Add(info);
            }
            rd.Close();
            return list;
        }
        public VoteTopicInfo GetTopic(int tid)
        {
            VoteTopicInfo info = new VoteTopicInfo();
            info.TID = 0;
            string SiteID = "0";
            if (!NetCMS.Global.Current.IsTimeout())
            {
                SiteID = NetCMS.Global.Current.SiteID;
            }
            string Sql = "Select [TID],[VID],[Title],[Type],[DisMode],[StartDate],[EndDate],[ItemMode],[SiteID],[AllowFillIn],[SeriesMode]";
            Sql += " From " + Pre + "Vote_Topic where TID = " + tid + " and SiteID='" + SiteID + "'";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            if (rd.Read())
            {
                info.TID = rd.GetInt32(0);
                info.VID = rd.GetInt32(1);
                info.Title = rd.GetString(2);
                if (!rd.IsDBNull(3)) info.Type = rd.GetByte(3);
                if (!rd.IsDBNull(5)) info.DisMode = rd.GetByte(4);
                if (!rd.IsDBNull(6)) info.StartDate = rd.GetDateTime(5);
                if (!rd.IsDBNull(7)) info.EndDate = rd.GetDateTime(6);
                if (!rd.IsDBNull(8)) info.ItemMode = rd.GetByte(7);
                if (!rd.IsDBNull(10)) info.SiteID = rd.GetString(8);
                info.AllowFillIn = rd.GetBoolean(9);
                info.SeriesMode = rd.GetByte(10);
            }
            rd.Close();
            return info;
        }
        public object GetLastVoteTimeByIP(int TID, string IPAddr, string UserNum)
        {
            string SiteID = "0";
            if (UserNum != null && UserNum.Trim() != string.Empty)
            {
                SiteID = NetCMS.Global.Current.SiteID;
            }
            string Sql = "select top 1 VoteTime from " + Pre + "vote_Manage where TID=" + TID + " and VoteIp=@VoteIp and SiteID='" + SiteID + "'";
            SqlParameter[] Param;
            if (UserNum != null && UserNum.Trim() != string.Empty)
            {
                Sql += " and UserNumber=@UserNumber";
                Param = new SqlParameter[]{new SqlParameter("@VoteIp", IPAddr),new SqlParameter("@UserNumber", UserNum)};
            }
            else
            { 
                Param = new SqlParameter[]{new SqlParameter("@VoteIp", IPAddr)};
            }
            Sql += " order by VoteTime desc";
            return DbHelper.ExecuteScalar(CommandType.Text, Sql, Param);
        }
        public string GetVoteItemName(int iid)
        {
            string Sql = "Select ItemName From " + Pre + "vote_Item where IID=" + iid + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            return Convert.ToString(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
        }

        public DataTable sel_voteByStr(string str, int flag)
        {
            #region
            string Sql = null;
            if (flag == 0)
            {
                Sql = "Select Title,TID From " + Pre + "Vote_Topic where Title like '%" + str + "%' and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 1)
            {
                Sql = "Select TID From " + Pre + "Vote_Topic where vid in(" + str + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 2)
            {
                Sql = "Select ClassName,VID From " + Pre + "vote_Class where ClassName like '%" + str + "%' and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
            #endregion
        }

        public int Del_voteManage(int RID, int flag)
        {
            #region
            string Sql = null;
            if (flag == 0)
            {
                Sql = "Delete From " + Pre + "vote_Manage where SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 1)
            {
                Sql = "Delete From " + Pre + "vote_Manage where rid=" + RID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 2)
            {
                Sql = "Delete From " + Pre + "vote_Item where iid=" + RID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 3)
            {
                Sql = "Delete From " + Pre + "vote_Steps where sid=" + RID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
            #endregion
        }

        public bool Del_voteSql(int VID, int flag)
        {
            #region 删除Vote相关信息
            string Sql = null;
            if (flag == 0)
            {
                Sql = "Delete From " + Pre + "vote_Class where vid=" + VID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 1)
            {
                Sql = "Delete From " + Pre + "Vote_Topic where vid=" + VID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 2)
            {
                Sql = "Delete From " + Pre + "vote_Item where TID=" + VID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 3)
            {
                Sql = "Delete From " + Pre + "vote_Steps where TIDS=" + VID + " or TIDU=" + VID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 4)
            {
                Sql = "Delete From " + Pre + "vote_Manage where TID=" + VID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 5)
            {
                Sql = "Delete From " + Pre + "Vote_Topic where tid=" + VID + " and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }

            int i = DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
            if (i == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
            #endregion
        }

        public void del_voteInfoSql(string CheckboxArray, bool flag)
        {
            #region
            string Sql = null;
            if (flag)
            {
                Sql = "Delete From " + Pre + "vote_Class where vid in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else
            {
                Sql = "Delete From " + Pre + "Vote_Topic where vid in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
            #endregion
        }

        public void del_strVoteSql(int tid, int flag)
        {
            #region
            string Sql = null;
            if (flag == 0)
            {
                Sql = "Delete From " + Pre + "vote_Item where TID in(" + tid + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 1)
            {
                Sql = "Delete From " + Pre + "vote_Steps where TIDS in(" + tid + ") or TIDU in(" + tid + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 2)
            {
                Sql = "Delete From " + Pre + "vote_Manage where TID in(" + tid + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
            #endregion
        }

        public bool del_VoteInfo(int flag)
        {
            #region 删除
            string Table = null;
            if (flag == 0)
            {
                Table = "vote_Class";
            }
            else if (flag == 1)
            {
                Table = "Vote_Topic";
            }
            else if (flag == 2)
            {
                Table = "vote_Item";
            }
            else if (flag == 3)
            {
                Table = "vote_Steps";
            }
            string Sql = "Delete From " + Pre + Table + " where SiteID='" + NetCMS.Global.Current.SiteID + "'";
            int i = DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
            if (i == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
            #endregion
        }

        public DataTable sel_voteInfo(int flag)
        {
            #region
            string Sql = null;
            if (flag == 0)//setItem.aspx
            {
                Sql = "Select a.VID,a.ClassName,b.TID,b.Title From " + Pre + "vote_Class as a inner join " + Pre + "Vote_Topic as b on a.VID=b.VID and a.SiteID='" + NetCMS.Global.Current.SiteID + "' and b.SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 1)//setParam.aspx
            {
                Sql = "Select * From " + Pre + "vote_Param";
            }
            else if (flag == 2)//setSteps.aspx
            {
                Sql = "Select TID,Title From " + Pre + "Vote_Topic where isSteps=1 and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 3)//setTitle.aspx
            {
                Sql = "Select VID,ClassName From " + Pre + "vote_Class where SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
            #endregion
        }

        public int del_InfoVote(int flag)
        {
            #region
            string Sql = null;
            if (flag == 0)
            {
                Sql = "Delete From " + Pre + "setSteps where SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 1)
            {
                Sql = "Delete From " + Pre + "Vote_Topic where SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
            #endregion
        }

        public int del_voteSql(string CheckboxArray, int flag)
        {
            #region
            string Sql = null;
            if (flag == 0)
            {
                Sql = "Delete From " + Pre + "Vote_Topic where tid in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 1)
            {
                Sql = "Delete From " + Pre + "vote_Item where TID in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 2)
            {
                Sql = "Delete From " + Pre + "vote_Steps where TIDS in(" + CheckboxArray + ") or TIDU in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 3)
            {
                Sql = "Delete From " + Pre + "vote_manage where TID in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 4)
            {
                Sql = "Delete From " + Pre + "vote_Steps where sid in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 5)
            {
                Sql = "Delete From " + Pre + "vote_Manage where rid in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            else if (flag == 6)
            {
                Sql = "Delete From " + Pre + "vote_Item where iid in(" + CheckboxArray + ") and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
            #endregion
        }
        public string GetUserName(string usernum)
        {//从会员表中取会员名
            string Sql = "Select UserName From " + Pre + "sys_User where UserNum=@UserNum and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            SqlParameter Param = new SqlParameter("@UserNum", usernum);
            return Convert.ToString(DbHelper.ExecuteScalar(CommandType.Text, Sql, Param));
        }
        #endregion

        #region setClass.aspx
        public int Add(string Str_ClassName, string Str_Description, string SiteID)
        {
            SqlParameter[] Param = new SqlParameter[3];
            Param[0] = new SqlParameter("@Str_ClassName", SqlDbType.NVarChar, 20);
            Param[0].Value = Str_ClassName;
            Param[1] = new SqlParameter("@Str_Description", SqlDbType.NVarChar, 50);
            Param[1].Value = Str_Description;
            Param[2] = new SqlParameter("@SiteID", SqlDbType.NVarChar, 12);
            Param[2].Value = SiteID;
            string Sql = "Insert into " + Pre + "vote_Class (ClassName,Description,SiteID) Values(@Str_ClassName,@Str_Description,@SiteID)";
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, Param);
        }
        public int Update_Str_InSql(string Str_ClassNameE, string Str_DescriptionE, int VID)
        {
            SqlParameter[] Param = new SqlParameter[3];
            Param[0] = new SqlParameter("@Str_ClassNameE", SqlDbType.NVarChar, 20);
            Param[0].Value = Str_ClassNameE;
            Param[1] = new SqlParameter("@Str_DescriptionE", SqlDbType.NVarChar, 50);
            Param[1].Value = Str_DescriptionE;
            Param[2] = new SqlParameter("@VID", SqlDbType.Int, 4);
            Param[2].Value = VID;
            string Sql = "Update " + Pre + "vote_Class Set ClassName=@Str_ClassNameE,Description=@Str_DescriptionE where vid=@VID  and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, Param);
        }
        #endregion

        #region setItem.aspx
        public int EditItem(VoteItemInfo info)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CmsConString);
            cn.Open();
            try
            {
                string Sql = "select count(IID) from " + Pre + "Vote_Item where ItemName=@ItemName";
                if (info.IID > 0)
                {
                    Sql += " and IID<>" + info.IID;
                }

                SqlParameter Pm = new SqlParameter("@ItemName", info.ItemName);
                int n = (int)DbHelper.ExecuteNonQuery(cn, CommandType.Text, Sql, Pm);
                if (n > 0)
                    throw new Exception("该投票标题已经存在!");
                Sql = "Insert into " + Pre + "Vote_Item ([TID],[ItemName],[ItemMode],[PicSrc],[DisColor],[VoteCount],[ItemDetail],[SiteID]) ";
                Sql += " Values (@TID,@ItemName,@ItemMode,@PicSrc,@DisColor,@VoteCount,@ItemDetail,@SiteID)";
                if (info.IID > 0)
                {
                    Sql = "Update " + Pre + "Vote_Item Set TID=@TID,ItemName=@ItemName,ItemMode=@ItemMode,";
                    Sql += "PicSrc=@PicSrc,DisColor=@DisColor,VoteCount=@VoteCount,ItemDetail=@ItemDetail,SiteID=@SiteID where SiteID='" + NetCMS.Global.Current.SiteID + "' and IID=" + info.IID;
                }
                SqlParameter[] Param = new SqlParameter[8];
                Param[0] = new SqlParameter("@TID", SqlDbType.Int);
                Param[0].Value = info.TID;
                Param[1] = new SqlParameter("@ItemName", SqlDbType.NVarChar, 200);
                Param[1].Value = info.ItemName;
                Param[2] = new SqlParameter("@ItemMode", SqlDbType.TinyInt);
                Param[2].Value = info.ItemMode;
                Param[3] = new SqlParameter("@PicSrc", SqlDbType.NVarChar, 200);
                if (info.PicSrc == string.Empty)
                    Param[3].Value = DBNull.Value;
                else
                    Param[3].Value = info.PicSrc;
                Param[4] = new SqlParameter("@DisColor", SqlDbType.NVarChar, 7);
                if (info.DisColor == string.Empty)
                    Param[4].Value = DBNull.Value;
                else
                    Param[4].Value = info.DisColor;
                Param[5] = new SqlParameter("@VoteCount", SqlDbType.Int);
                Param[5].Value = info.VoteCount;
                Param[6] = new SqlParameter("@ItemDetail", SqlDbType.NVarChar, 255);
                if (info.ItemDetail == string.Empty)
                    Param[6].Value = DBNull.Value;
                else
                    Param[6].Value = info.ItemDetail;
                Param[7] = new SqlParameter("@SiteID", SqlDbType.NVarChar, 12);
                Param[7].Value = info.SiteID;
                return DbHelper.ExecuteNonQuery(cn, CommandType.Text, Sql, Param);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }

        }
        #endregion

        #region setParam.aspx
        public int Update_Str_InSqls(int IPtime, byte IsReg, string IpLimit)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CmsConString);
            cn.Open();
            try
            {
                string Sql = "select count(SysID) from " + Pre + "vote_Param where SiteID='" + NetCMS.Global.Current.SiteID + "'";
                int n = (int)DbHelper.ExecuteScalar(cn, CommandType.Text, Sql, null);
                if (n > 0)
                    Sql = "Update " + Pre + "vote_Param Set IPtime=@IPtime,IsReg=@IsReg,IpLimit=@IpLimit where SiteID=@SiteID";
                else
                {
                    Sql = "insert into " + Pre + "vote_Param (IPtime,IsReg,IpLimit,SiteID) values (";
                    Sql += "@IPtime,@IsReg,@IpLimit,@SiteID)";
                }
                SqlParameter[] Param = new SqlParameter[4];
                Param[0] = new SqlParameter("@IPtime", SqlDbType.Int);
                Param[0].Value = IPtime;
                Param[1] = new SqlParameter("@IsReg", SqlDbType.TinyInt);
                Param[1].Value = IsReg;
                Param[2] = new SqlParameter("@IpLimit", SqlDbType.NText);
                if (IpLimit.Trim() == string.Empty)
                    Param[2].Value = DBNull.Value;
                Param[2].Value = IpLimit;
                Param[3] = new SqlParameter("@SiteID", SqlDbType.NVarChar, 12);
                Param[3].Value = NetCMS.Global.Current.SiteID;
                return DbHelper.ExecuteNonQuery(cn, CommandType.Text, Sql, Param);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        #endregion

        #region setSteps.aspx
        public int sel_Str_CheckSql(string Str_vote_CNameSe, string Str_vote_CNameUse)
        {
            string Sql = "Select count(*) From " + Pre + "vote_Steps Where TIDS='" + Str_vote_CNameSe + "' and TIDU='" + Str_vote_CNameUse + "' and SiteID='" + NetCMS.Global.Current.SiteID + "'";
            return (int)DbHelper.ExecuteScalar(CommandType.Text, Sql, null);
        }
        public int Add_Str_InSql(string Str_vote_CNameSe, string Str_vote_CNameUse, string Str_StepsN, string SiteID)
        {
            string Sql = "Insert into " + Pre + "vote_Steps (TIDS,TIDU,Steps,SiteID) Values('" + Str_vote_CNameSe + "','" + Str_vote_CNameUse + "','" + Str_StepsN + "','" + SiteID + "')";
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
        }

        public int update_voteSteps(string Str_votecnameEditse, string Str_votecnameEditue, string Str_NumEdit, int SID)
        {
            string Sql = "Update " + Pre + "vote_Steps Set TIDS='" + Str_votecnameEditse + "',TIDU='" + Str_votecnameEditue + "',Steps='" + Str_NumEdit + "'  where SiteID='" + NetCMS.Global.Current.SiteID + "' and sid=" + SID;
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
        }
        #endregion

        #region setTitle.aspx
        public int EditTopic(VoteTopicInfo info)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CmsConString);
            cn.Open();
            try
            {
                string Sql = "select count(TID) from " + Pre + "Vote_Topic where Title=@Title";
                if (info.TID > 0)
                {
                    Sql += " and TID<>" + info.TID;
                }
                SqlParameter Pm = new SqlParameter("@Title", info.Title);
                int n = (int)DbHelper.ExecuteNonQuery(cn, CommandType.Text, Sql, Pm);
                if (n > 0)
                    throw new Exception("该投票标题已经存在!");
                Sql = "Insert into " + Pre + "Vote_Topic (VID,Title,Type,DisMode,StartDate,EndDate,ItemMode,SiteID,AllowFillIn,SeriesMode) ";
                Sql += " Values (@VID,@Title,@Type,@DisMode,@StartDate,@EndDate,@ItemMode,@SiteID,@AllowFillIn,@SeriesMode)";
                if (info.TID > 0)
                {
                    Sql = "Update " + Pre + "Vote_Topic Set VID=@VID,Title=@Title,Type=@Type,DisMode=@DisMode,";
                    Sql += "StartDate=@StartDate,EndDate=@EndDate,ItemMode=@ItemMode,AllowFillIn=@AllowFillIn,SeriesMode=@SeriesMode where SiteID='" + NetCMS.Global.Current.SiteID + "' and tid=" + info.TID;
                }
                SqlParameter[] Param = new SqlParameter[10];
                Param[0] = new SqlParameter("@VID", SqlDbType.Int);
                Param[0].Value = info.VID;
                Param[1] = new SqlParameter("@Title", SqlDbType.NVarChar, 50);
                Param[1].Value = info.Title;
                Param[2] = new SqlParameter("@Type", SqlDbType.TinyInt);
                Param[2].Value = info.Type;
                Param[3] = new SqlParameter("@SeriesMode", SqlDbType.TinyInt);
                Param[3].Value = info.SeriesMode;
                Param[4] = new SqlParameter("@DisMode", SqlDbType.TinyInt);
                Param[4].Value = info.DisMode;
                Param[5] = new SqlParameter("@StartDate", SqlDbType.DateTime);
                Param[5].Value = info.StartDate;
                Param[6] = new SqlParameter("@EndDate", SqlDbType.DateTime);
                Param[6].Value = info.EndDate;
                Param[7] = new SqlParameter("@ItemMode", SqlDbType.TinyInt);
                Param[7].Value = info.ItemMode;
                Param[8] = new SqlParameter("@SiteID", SqlDbType.NVarChar, 12);
                Param[8].Value = info.SiteID;
                Param[9] = new SqlParameter("@AllowFillIn", SqlDbType.Bit);
                if (info.AllowFillIn)
                    Param[9].Value = 1;
                else
                    Param[9].Value = 0;
                return DbHelper.ExecuteNonQuery(cn, CommandType.Text, Sql, Param);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        #endregion
        public void add_voteManage(int[] SelIID, int tid, string othercontent, string strvip, string UserNum)
        {
            string Sql;
            SqlConnection cn = new SqlConnection(DBConfig.CmsConString);
            cn.Open();
            if (othercontent.Trim() != string.Empty)
            {
                Sql = "select Type,AllowFillIn from " + Pre + "Vote_Topic where TID=" + tid;
                IDataReader rd = DbHelper.ExecuteReader(cn, CommandType.Text, Sql, null);
                if (rd.Read())
                {
                    if (!rd.GetBoolean(1))
                        othercontent = string.Empty;
                    if (rd.GetByte(0) == 0 && SelIID.Length > 0)
                        othercontent = string.Empty;
                }
                rd.Close();
            }
            SqlTransaction tran = cn.BeginTransaction();
            try
            {
                foreach (int id in SelIID)
                {
                    if (id == 0 && othercontent.Trim() == string.Empty)
                        continue;
                    Sql = "Insert into " + Pre + "vote_Manage (IID,TID,OtherContent,VoteIp,VoteTime,UserNumber,SiteID) Values (@IID,@TID,@OtherContent,@VoteIp,'" + DateTime.Now + "',@UserNumber,@SiteID)";
                    SqlParameter[] Param = new SqlParameter[6];
                    Param[0] = new SqlParameter("@IID", SqlDbType.Int);
                    Param[0].Value = id;
                    Param[1] = new SqlParameter("@TID", SqlDbType.Int);
                    Param[1].Value = tid;
                    Param[2] = new SqlParameter("@OtherContent", SqlDbType.NVarChar, 50);
                    if (othercontent.Trim() == string.Empty)
                        Param[2].Value = DBNull.Value;
                    else
                        Param[2].Value = othercontent;
                    Param[3] = new SqlParameter("@VoteIp", SqlDbType.NVarChar, 15);
                    Param[3].Value = strvip;
                    Param[4] = new SqlParameter("@UserNumber", SqlDbType.NVarChar, 15);
                    if (UserNum == null || UserNum.Trim() == string.Empty)
                        Param[4].Value = DBNull.Value;
                    else
                        Param[4].Value = UserNum;
                    Param[5] = new SqlParameter("@SiteID", SqlDbType.NVarChar, 12);
                    if (NetCMS.Global.Current.IsTimeout())
                        Param[5].Value = "0";
                    else
                        Param[5].Value = NetCMS.Global.Current.SiteID;
                    DbHelper.ExecuteNonQuery(CommandType.Text, Sql, Param);
                }
                tran.Commit();
            }
            catch
            {
                tran.Rollback();
                throw;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        public int sel_IP()
        {
            int PageError = 0;
            string IPLIST = "";
            string LoginIP = NetCMS.Global.Current.ClientIP;
            string IPSQL = "select IpLimit from " + Pre + "vote_Param";
            object IPDT = DbHelper.ExecuteScalar(CommandType.Text, IPSQL, null);
            if (IPDT != null && IPDT != DBNull.Value)
            {
                IPLIST = IPDT.ToString();
                if (IPLIST != "" && IPLIST != null)
                {
                    int IPTF = 0;
                    string[] arrIP = IPLIST.Split(new Char[] { '|' });
                    for (int i = 0; i < arrIP.Length; i++)
                    {
                        if (arrIP[i].IndexOf('*') >= 0)
                        {
                            string strIP = arrIP[i];
                            string[] IPR = strIP.Split(new Char[] { '.' });
                            string[] IPR1 = LoginIP.Split(new Char[] { '.' });
                            if (IPR[2] == "*")
                            {
                                if (IPR1[0] + IPR1[1] == IPR[0] + IPR[1])
                                {
                                    IPTF = 1;
                                }
                            }
                            else if (IPR[3] == "*")
                            {
                                if (IPR1[0] + IPR1[1] + IPR1[2] == IPR[0] + IPR[1] + IPR[2])
                                {
                                    IPTF = 1;
                                }
                            }
                        }
                        else
                        {
                            if (arrIP[i].ToString() == LoginIP.ToString())
                            {
                                IPTF = 1;
                            }
                        }
                    }
                    if (IPTF == 1)
                    {
                        PageError = 1;
                    }
                }

            }
            return PageError;
        }

        public DataTable GetVoteStat(int tid, out int totalvote)
        {
            totalvote = 0;
            SqlConnection cn = new SqlConnection(DBConfig.CmsConString);
            cn.Open();
            try
            {
                string SiteID = "0";
                try
                {
                    SiteID = NetCMS.Global.Current.SiteID;
                }
                catch
                {

                }
                string Sql = "select count(rid) from " + Pre + "vote_Manage where tid=" + tid;
                totalvote = Convert.ToInt32(DbHelper.ExecuteScalar(cn, CommandType.Text, Sql, null));
                Sql = "select sum(VoteCount) from " + Pre + "vote_Item where tid=" + tid;
                object obj = DbHelper.ExecuteScalar(cn, CommandType.Text, Sql, null);
                if (obj != null && obj != DBNull.Value)
                    totalvote += (int)obj;
                Sql = "SELECT a.TID, a.IID, a.ItemName, a.ItemMode, a.PicSrc, a.DisColor,";
                Sql += "a.VoteCount, a.ItemDetail, COUNT(b.RID) AS Result FROM " + Pre + "vote_Item a LEFT OUTER JOIN ";
                Sql += Pre + "vote_manage b ON a.TID = b.TID AND a.IID = b.IID where a.tid=" + tid;
                Sql += " GROUP BY a.TID, a.IID, a.ItemName, a.ItemMode, a.PicSrc, a.DisColor,a.VoteCount, a.ItemDetail";
                return DbHelper.ExecuteTable(cn, CommandType.Text, Sql, null);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
    }
}
/*

*/